package com.java2007.zhangyu.restaurant.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.java2007.zhangyu.restaurant.constant.ServletConstant;
import com.java2007.zhangyu.restaurant.controller.BaseServlet;
import com.java2007.zhangyu.restaurant.entity.PageBean;
import com.java2007.zhangyu.restaurant.pojo.DinnerTable;
import com.java2007.zhangyu.restaurant.pojo.Food;
import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.FoodService;
import com.java2007.zhangyu.restaurant.service.FoodTypeService;
import com.java2007.zhangyu.restaurant.service.TableService;
import com.java2007.zhangyu.restaurant.service.impl.FoodServiceImpl;
import com.java2007.zhangyu.restaurant.service.impl.FoodTypeServiceImpl;
import com.java2007.zhangyu.restaurant.service.impl.TableServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 前端控制器
 * @author zy
 * @Version
 * @date 2021/01/29
 */
@WebServlet("/front")
public class FrontController extends BaseServlet {
    private TableService tableService = new TableServiceImpl();
    private FoodService foodService = new FoodServiceImpl();
    private FoodTypeService foodTypeService = new FoodTypeServiceImpl();

    /**
     * 在首页展示所有的空闲餐桌
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String queryTableByStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //调用业务层查询餐桌状态为未预定的餐桌
        List<DinnerTable> tables = tableService.queryByStatus(0);
        //将查询到的信息放入request域中
        request.setAttribute("tables", tables);

        //创建变量 session
        HttpSession session = request.getSession();

        //加载菜类别列表
        List<FoodType> types = (List<FoodType>) session.getAttribute("front_types");

        //判断是否为空，若为空，查询所有并将查询结果放入session中
        if(types == null || types.size() == 0) {
            types = foodTypeService.queryAll();
            session.setAttribute("front_types", types);
        }

        //转发
        return "forward:/front/index.jsp";
    }

    /**
     * 菜单分页展示菜品信息
     *  每页显示 6 条数据
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String queryFoodByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //保存餐桌ID
        String tableId = request.getParameter("tableId");
        if(tableId != null) {
            request.getSession().setAttribute("tableId", Integer.parseInt(tableId));
            DinnerTable table = new DinnerTable();
            table.setDtId(Integer.parseInt(tableId));
            table.setDtState("1");
            tableService.update(table);
        }

        //查询条件封装成一个菜品对象
        Food food = new Food();

        String ftIdStr = request.getParameter("ftId");
        if (!StringUtils.isEmpty(ftIdStr)) {
            //不为空，转为 int； 封装条件到food实体中
            int ftId = Integer.parseInt(ftIdStr);
            if(ftId > 0) {
                food.setFtId(ftId);
            }
        }

        String fNameStr = request.getParameter("fName");
        if(!StringUtils.isEmpty(fNameStr)){
            //不为空，去掉前后空格，封装条件到food实体中
            food.setfName(fNameStr.trim());
            request.setAttribute("fName", food.getfName());
        }

        String pageNoStr = request.getParameter("pageNo");
        if (StringUtils.isEmpty(pageNoStr)) {
            pageNoStr = "1";
        }

        int pageNo = Integer.parseInt(pageNoStr);
        PageBean<Food> pageBean = foodService.queryPage(pageNo, PageBean.SIZE, food);
        request.setAttribute("pageBean", pageBean);

        return "forward:/front/detail/caidan.jsp";

    }

    /**
     * 展示菜品详细信息
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String queryFoodById(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String fIdStr = request.getParameter("fId");

        if(!StringUtils.isEmpty(fIdStr)){

            int fId = Integer.parseInt(fIdStr);

            Food food = foodService.queryByFoodId(fId);

            request.setAttribute("food", food);

            return ServletConstant.PRE_FORWARD +  "/front/detail/caixiangxi.jsp";
        } else {
            return "查无此菜";
        }

    }
}
